package com.ABdolphin.forum.controller;

import com.ABdolphin.forum.common.AppResult;
import com.ABdolphin.forum.common.ResultCode;
import com.ABdolphin.forum.exception.ApplicationException;
import com.ABdolphin.forum.model.Board;
import com.ABdolphin.forum.services.IBoardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/board")
@Tag(name = "板块接口")
public class BoardController {
    @Resource
    private IBoardService boardService;
    //从配置文件中读取数据,如果没有数据,默认值为9
    @Value("${ABdolphin-forum.index.board-num:9}")
    private Integer indexBoardNum;

    @Operation(summary = "首页板块列表")
    @GetMapping("/topList")
    public AppResult<List<Board>> topList(){
        log.info("首页板块个数为: "+indexBoardNum);
        List<Board> boards = boardService.selectByNum(indexBoardNum);
        if (boards==null){
            boards=new ArrayList<>();
        }
        return AppResult.success(boards);
    }
    @GetMapping("/getById")
    @Operation(summary = "获得版块信息")
    public AppResult<Board> getById(@Parameter(name = "id",description = "版块id")@RequestParam("id")@NotNull Long id){
        Board board = boardService.selectById(id);
        if (board==null||board.getDeleteState()==1){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        return AppResult.success(board);
    }

}














